#include "Fl_Wait.h"

namespace FPTK{

Fl_Wait::Fl_Wait(int x, int y, int w, int h, const char *l):Fl_Progress(x,y,w,h,l)
{
   var_tempo=0;
   step=2;
   pos=0;
}

Fl_Wait::~Fl_Wait()
{
}

void Fl_Wait::SetTempo(unsigned int _var_tempo)
{
   var_tempo=_var_tempo;
}

int Fl_Wait::NbStep(int _step)
{
   if(_step>=2)
   {
      step=_step;
      return 0;
   }
   return -1;
}

void Fl_Wait::Tempo()
{
    clock_t goal = var_tempo + clock();
    while (goal > clock());
    pos=(pos+1)%step;
    redraw();
}

void Fl_Wait::redraw()
{
   draw();
}

void Fl_Wait::draw()
{
value(0);
  int progress1,progress2,progress;
  int	bx, by, bw, bh;	// Box areas...
  int	tx, tw;		// Temporary X + width


  // Get the box borders...
  bx = Fl::box_dx(box());
  by = Fl::box_dy(box());
  bw = Fl::box_dw(box());
  bh = Fl::box_dh(box());

  tx = x() + bx;
  tw = w() - bw;

  // Draw the progress bar...
  if (maximum() > minimum())
  {
    progress1 = (int)(tw * (pos)/step);
    progress2 = (int)(tw * (pos+1)/step);//(value_ - minimum_) / (maximum_ - minimum_) + 0.5f);
    progress = (int)(tw * (pos+1)/step);//(value_ - minimum_) / (maximum_ - minimum_) + 0.5f);
  }
  else
  {
     progress=0;
    progress1 = 0;
    progress2=0;
  }

  // Draw the box and label...
  if (progress > 0) {
    Fl_Color c = labelcolor();

    fl_clip(x(), y(), w() + progress1, h());
      draw_box(box(), x(), y(), w(), h(), active_r() ? color() : fl_inactive(color()));
      draw_label(tx, y() + by, tw, h() - bh);
    fl_pop_clip();



    labelcolor(fl_contrast(labelcolor(), color2()));

    fl_clip(x()+progress1, y(), progress+bw, h());
      draw_box(box(), x(), y(), w(), h(), active_r() ? color2() : fl_inactive(color2()));
      draw_label(tx, y() + by, tw, h() - bh);
    fl_pop_clip();

    labelcolor(c);

    fl_clip(tx + progress, y(), w() - progress, h());
      draw_box(box(), x(), y(), w(), h(), active_r() ? color() : fl_inactive(color()));
      draw_label(tx, y() + by, tw, h() - bh);
    fl_pop_clip();
  } else {
    //draw_box(box(), x(), y(), w(), h(), color());
    //draw_label(tx, y() + by, tw, h() - bh);
  }
}

} //end namespace FPTK